package com.itita.ww2.data;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class CityConstructionCfg {
	public static boolean isLoaded;
	public static final String specialProductRefreshTask = "city_sprt";

	private static ISFSObject config; // 系统配置
	private static ISFSObject attribute; // 产业配置
	private static ISFSObject specialCfg; // 特产配置

	private static RefreshTime refreshTime;

	public static final void init(ISFSObject data) throws Exception {
		config = data.getSFSObject("config");
		attribute = data.getSFSObject("attribute");
		specialCfg = data.getSFSObject("special_const");
		setRefreshTime();
		isLoaded = true;
	}

	private static final void setRefreshTime() throws Exception {
		refreshTime = new RefreshTime();
		String timeline = config.getUtfString("refreshTime");
		Pattern p = Pattern.compile("(\\d{1,2}):(\\d{1,2})\\s+(.*)");
		Matcher m = p.matcher(timeline);
		if (!m.find()) {
			throw new Exception("Time Config Error!");
		}
		refreshTime.hour = Integer.valueOf(m.group(1));
		refreshTime.minute = Integer.valueOf(m.group(2));
		refreshTime.zoneId = m.group(3);
	}

	/**
	 * 产出间隔时间(s)
	 */
	public static final int getOutInterval() {
		return config.getInt("outInterval");
	}

	public static final RefreshTime getRefreshTime() {
		return refreshTime;
	}

	/**
	 * 产出方案倍率
	 */
	public static final float getSchemeRatio(SchemeType type) {
		return config.getSFSArray("scheme").getFloat(type.ordinal());
	}

	public static enum SchemeType {
		NORMAL, CAPTICAL;
		public static SchemeType valueOf(int i) {
			for (SchemeType type : values()) {
				if (type.ordinal() == i) {
					return type;
				}
			}
			return NORMAL;
		}
	}

	/**
	 * 获取单种最大购买量
	 */
	public static final int getSingleBuyLimit(int lv) {
		return config.getSFSArray("limit").getInt(lv);
	}

	/**
	 * 获取购买所需资源
	 */
	public static final ISFSObject getBuyResource(String code) {
		return attribute.getSFSObject(code).getSFSArray("res_list").getSFSObject(0);
	}

	/**
	 * 获取lv等级升级所需资源
	 */
	public static final ISFSObject getUpgradeResource(String code, int lv) {
		return attribute.getSFSObject(code).getSFSArray("res_list").getSFSObject(lv);
	}

	/**
	 * 获取产出类型(资源类型代码)
	 */
	public static final String getOutputType(String code) {
		return attribute.getSFSObject(code).getUtfString("output_type");
	}

	/**
	 * 根据产业等级获取产出数值
	 */
	private static final int getOutputByLv(String code, int lv) {
		return attribute.getSFSObject(code).getSFSArray("output").getInt(lv - 1);
	}

	/**
	 * 获取产业的实际产出
	 * @param needScheme 是否需要计算方案
	 * @param scheme 方案
	 */
	public static final int getOutputReal(String code, int lv, int scheme) {
		SchemeType type = SchemeType.valueOf(scheme);
		float ratio = getSchemeRatio(type);
		return Math.round(getOutputByLv(code, lv) * ratio);
	}

	private static final ISFSObject getCitySpecialProduct(int blockId, int cityId) {
		return specialCfg.getSFSObject("B" + blockId + "c" + cityId);
	}

	/**
	 * 获取城池固定特产
	 */
	public static final ISFSObject getCityFixedSpecialProduct(int blockId, int cityId) {
		return getCitySpecialProduct(blockId, cityId).getSFSObject("fixed_output");
	}

	/**
	 * 获取城池周期特产
	 * @param weekShortText 星期的简称(前三位)
	 */
	public static final ISFSObject getCityWeekSpecialProduct(int blockId, int cityId, String weekShortText) {
		String key = weekShortText.toLowerCase() + "_output";
		ISFSObject obj = getCitySpecialProduct(blockId, cityId);
		if (!obj.containsKey(key)) {
			return SFSObject.newInstance();
		}
		return obj.getSFSObject(key);
	}

	public static class RefreshTime {
		private int hour;
		private int minute;
		private String zoneId;

		public final int getHour() {
			return hour;
		}

		public final int getMinute() {
			return minute;
		}

		public final String getZoneId() {
			return zoneId;
		}
	}
}
